{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using `brainrender` in jupyter notebooks\n",
    "\n",
    "There's three ways that you can render a `brainrender` scene in Jupyter:\n",
    "\n",
    "1. You can have it pop up in a new window (recomended)\n",
    "2. You can have embedded in the notebook\n",
    "3. You can have it embedded a a ipywidget\n",
    "\n",
    "Note that option 2 and 3 don't support all of `brainrender`'s functionality.\n",
    "As such option 1 is recomended.\n",
    "\n",
    "If you get an error while running this notebook, restart the kernel and run only one\n",
    "of the three rendering options. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from brainrender import Scene\n",
    "from vedo import embedWindow, Plotter, show  # <- this will be used to render an embedded scene \n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Render as popup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedWindow(None)  # <- this will make your scene popup\n",
    "\n",
    "from brainrender import Scene\n",
    "\n",
    "popup_scene = Scene(title='popup')\n",
    "\n",
    "popup_scene.add_brain_region('MOs')\n",
    "\n",
    "popup_scene.render()  # press 'Esc' to close!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Embeddded scene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedWindow('k3d')  # <- this will make your scene embed with k3d\n",
    "\n",
    "\n",
    "# Create a scene\n",
    "scene = Scene(title='Embedded')  # note that the title will not actually display\n",
    "scene.add_brain_region('MOs')\n",
    "\n",
    "# Make sure it gets embedded in the window\n",
    "scene.jupyter = True\n",
    "\n",
    "# scene.render now will prepare the scene for rendering, but it won't render anything yet\n",
    "scene.render()\n",
    "\n",
    "#  to actually display the scene we use `vedo`'s `show` method to show the scene's actors\n",
    "plt = Plotter()\n",
    "plt.show(*scene.renderables)  # same as vedo.show(*scene.renderables)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Widget scene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedWindow('itkwidgets')  # use itk widgets\n",
    "\n",
    "# Create a scene\n",
    "scene = Scene(title=\"brain regions\", inset=False)\n",
    "\n",
    "# Add the whole thalamus in gray\n",
    "scene.add_brain_region(\"TH\", alpha=0.15)\n",
    "\n",
    "# Add VAL nucleus in wireframe style with the allen color\n",
    "val = scene.add_brain_region(\"VAL\")\n",
    "val.wireframe()\n",
    "\n",
    "scene.render()\n",
    "\n",
    "view(scene.plotter.show(*scene.renderables))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
